﻿Imports System.IO
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            'For x As Integer = SlimDX.ObjectTable.Objects.Count - 1 To 0 Step -1
            'SlimDX.ObjectTable.Objects(x).Dispose()
            'Next
            DirectX.xDevice.Dispose()
        End Sub
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Directory.Exists(Application.Info.DirectoryPath & "\old\") Then
                Directory.Delete(Application.Info.DirectoryPath & "\old\", True)
            End If
            If e.CommandLine.Count = 0 Then
                If Update.CheckUpdates Then
                    Try
                        Process.Start(Application.Info.AssemblyName & ".exe", "updated")
                        e.Cancel = True
                    Catch ex As Exception
                        e.Cancel = False
                    End Try
                Else
                    e.Cancel = False
                End If
            End If
        End Sub

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance

        End Sub

        'Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        '    If e.Exception.GetType Is GetType(System.IO.FileNotFoundException) Then
        '        If e.Exception.Message.Contains("SlimDX") Then
        '            If MessageBox.Show("Please make sure you have SlimDX End User Runtime installed. Press OK to goto the download page or Cancel to Exit.", "SlimDX", MessageBoxButtons.OKCancel) = MsgBoxResult.Ok Then
        '                Process.Start("http://code.google.com/p/SlimDX/wiki/Downloads?tm=2")
        '            End If
        '            Exit Sub
        '        End If
        '    End If
        '    RemoveHandler Application.UnhandledException, AddressOf Me.MyApplication_UnhandledException
        '    Throw e.Exception
        'End Sub
    End Class
End Namespace

